<?php

namespace app\index\controller;
 

/**
 * 日志管理
 *
 * @icon fa fa-circle-o
 */
class Log extends User
{
    
    /**
     * Log模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Log');
        $this->view->assign("logTypeList", $this->model->getLogTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index()
    {  
        return;
    }
    public function log_list($log_time='')
    { 

        $where = 'user_id = '.$this->auth->id;
        if($log_time){
            $chektime = $log_time;  
            $where .= " and log_time between '".strtotime($log_time." 00:00:00")."' and '".strtotime($log_time." 23:59:59")."' ";
        }else
            $chektime = '选择时间';
 
        // list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $sort = 'id';
        $order = 'desc';
        $offset = 0;
        $limit = 100;
        $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

        $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

        $list = collection($list)->toArray();
        $result = array("total" => $total, "rows" => $list);
        $this->view->assign("result", $result); 
        $this->view->assign("chektime", $chektime); 
        return $this->view->fetch();
    }
    public function log_detail($id){ 
        $row = $this->model->get($id)->toArray();
        $this->view->assign("result", $row);  
        return $this->view->fetch();
    }
    public function log_add()
    {  
        if ($this->request->isPost())
        {
            $params = $this->request->post();
            try {
                $params['user_id'] = $this->auth->id;
                $params['user_name'] = $this->auth->username;
                $params['log_time'] = time();
                $site_name =  model('Site')->where(["id"=>$params['site_id']])->find()->toArray();
                $params['site_name'] = $site_name['building_name'];
                $result = $this->model->save($params);
                if ($result !== false)
                {
                    $this->success();
                }
                else
                {
                    $this->error($this->model->getError());
                }
            } catch (Exception $e) {
                $this->error($e->getMessage());
            } 
        } 
        $list = model('Site')->where(["building_delete"=>0])->select(); 
        if($list){ 
            foreach ($list as $key => $value) {
                $siteList[$value['id']] = $value['building_name']; 
            } 
            $this->view->assign("siteList",$siteList);
        }else{
            $this->error('暂无工地数据');
        }
        return $this->view->fetch();
    }
    public function log_edit($id ='')
    {  
        $row = array();
        if ($this->request->isPost())
        {
            $params = $this->request->post();
            try {
                $params['user_id'] = $this->auth->id;
                $params['user_name'] = $this->auth->username;
                $params['log_time'] = time();
                $site_name =  model('Site')->where(["id"=>$params['site_id']])->find()->toArray();
                $params['site_name'] = $site_name['building_name'];
                $result = $this->model->save($params,$id);
                if ($result !== false)
                {
                    $this->success();
                }
                else
                {
                    $this->error($this->model->getError());
                }
            } catch (Exception $e) {
                $this->error($e->getMessage());
            } 
        }
        if($id){
            $row = $this->model->get($id)->toArray();
        }
        $list = model('Site')->where(["building_delete"=>0])->select(); 
        if($list){ 
            foreach ($list as $key => $value) {
                $siteList[$value['id']] = $value['building_name']; 
            } 
            $this->view->assign("siteList",$siteList);
        }else{
            $this->error('暂无工地数据');
        }
        $this->view->assign("result", $row);  
        return $this->view->fetch();
    }
 
}
